package com.bloom.opensourceframework.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.bloom.opensourceframework.R;
import com.bloom.opensourceframework.adapter.FunctionsAdapter;
import com.bloom.opensourceframework.echart.EChartExampleActivity;
import com.bloom.opensourceframework.eventbus.ui.EventBusExampleActivity;
import com.bloom.opensourceframework.eventbus.entity.MessageEvent;
import com.bloom.opensourceframework.lottie.ui.LottieExampleActivity;
import com.bloom.opensourceframework.okhttp.ui.OkHttpExampleActivity;
import com.bloom.opensourceframework.retrofit.ui.RetrofitExampleActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

/**
 * Created by bloom on 2018/5/3.
 */

public class MainActivity extends AppCompatActivity implements FunctionsAdapter.ItemClickListener {

    private RecyclerView mRecyclerView;
    private FunctionsAdapter mFunctionsAdapter;
    private List<String> mDatas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initActionBar();
        initView();
    }

    private void initActionBar() {
        Toolbar tb = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(tb);
        setTitle("Open Source");
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rcv);
        mDatas = Arrays.asList(getResources().getStringArray(R.array.main_array));
        mFunctionsAdapter = new FunctionsAdapter(this, mDatas);
        mRecyclerView.setAdapter(mFunctionsAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mFunctionsAdapter.setOnItemClickListener(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(MessageEvent event) {
        System.out.print(event.message);
        Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void itemClick(String func) {
        switch (func) {
            case "OKHttp":
                jumpOkHttpExample();
                break;
            case "Retrofit":
                jumpRetrofitExample();
                break;
            case "EventBus":
                jumpEventBusExample();
                break;
            case "Lottie":
                jumpLottieExample();
                break;
            case "ECharts":
                jumpEChartsExample();
                break;
            default:
                break;
        }
    }

    private void jumpOkHttpExample() {
        startActivity(new Intent(this, OkHttpExampleActivity.class));
    }

    private void jumpRetrofitExample() {
        startActivity(new Intent(this, RetrofitExampleActivity.class));
    }

    private void jumpEventBusExample() {
        startActivity(new Intent(this, EventBusExampleActivity.class));
    }

    private void jumpLottieExample() {
        startActivity(new Intent(this, LottieExampleActivity.class));
    }

    private void jumpEChartsExample() {
        startActivity(new Intent(this, EChartExampleActivity.class));
    }
}
